﻿using GameServer.Core;
using GameServer.Core.FSM;
using GameServer.Mgr;
using GameServer.Model;
using Serilog;
using Summer;

namespace GameServer.AI
{
    public class MonsterAI : AIBase
    {
        public FsmSystem<Param> fsmSystem;
        public MonsterAI(Monster Owner) : base(Owner)
        {
            Param param = new Param();
            param.Owner = Owner;
            fsmSystem = new FsmSystem<Param>(param);
            //fsmSystem.AddState("walk",new WalkState());
            //fsmSystem.AddState("chase", new ChaseState());
            //fsmSystem.AddState("goback", new GobackState());
        }

        public override void Update()
        {
            fsmSystem?.Update();
        }
        public class Param
        {
            public Monster Owner;
            public Random rand = new Random();
        }

        //class WalkState:State<Param>
        //{
        //    float lastTime=Time.time;
        //    float waitTime = 10f;  //等待时间
        //    public override void OnEnter()
        //    {
        //        P.Owner.StopMove();
        //    }
        //    public override void OnUpdate()
        //    {
        //        var chr=EntityManager.Instance.GetNearest<Character>(P.Owner.Space.Id, P.Owner.Position, P.Owner.ViewRange);
        //        //Log.Information("最近的目标是{0}",chr);
        //        if(chr!=null)
        //        {
        //            P.Owner.target = chr;
        //            fsm.ChangeState("chase");
        //            return;
        //        }
        //        if(P.Owner.state==Proto.EntityState.Idle)
        //        {
        //            if(lastTime+ waitTime < Time.time)
        //            {
        //                lastTime=Time.time;
        //                waitTime = (P.rand.NextSingle() * 5f) + 10f;
        //                var target = P.Owner.RandomPointWithBirth(P.Owner.MoveRange);
        //                P.Owner.MoveTo(target);
        //                //移动到随机位置
        //            }
        //        }
        //    }
        //}

        //class ChaseState : State<Param>
        //{
        //    public override void OnUpdate()
        //    {
        //        if (P.Owner.target == null)
        //        {
        //            P.Owner.target = null;
        //            fsm.ChangeState("walk");
        //            return;
        //        }
        //        //自身与出生点的距离
        //        float m = Vector3.Distance(P.Owner.initPosition, new Vector3(P.Owner.Position.x, 0, P.Owner.Position.z));
                
        //        //自身和目标的距离
        //        float n = Vector3.Distance(new Vector3(P.Owner.Position.x, 0, P.Owner.Position.z), new Vector3(P.Owner.target.Position.x, 0, P.Owner.target.Position.z));
        //        if (m > P.Owner.ChaseRange || n > P.Owner.ViewRange)
        //        {
        //            //返回出生点
        //            fsm.ChangeState("goback");
        //            return;
        //        }
        //        if (n < 1200)
        //        {
        //            if (P.Owner.state == Proto.EntityState.Move|| P.Owner.state == Proto.EntityState.Run)
        //                P.Owner.StopMove();
        //            Log.Information("发起攻击");
        //        }
        //        else
        //        {
        //            P.Owner.MoveTo(new Vector3(P.Owner.target.Position.x, P.Owner.target.Position.y, P.Owner.target.Position.z),true);
        //        }
        //    }
        //}

        //class GobackState : State<Param>
        //{
        //    public override void OnEnter()
        //    {
        //        P.Owner.MoveTo(P.Owner.initPosition);
        //    }
        //    public override void OnUpdate()
        //    {
        //        var mon = P.Owner;
        //        P.Owner.state = Proto.EntityState.Move;
        //        if (Vector3.Distance(P.Owner.initPosition, new Vector3(P.Owner.Position.x, P.Owner.Position.y, P.Owner.Position.z)) < 100)
        //            fsm.ChangeState("walk");
        //    }
        //}
    }
}
